package com.rtwo.trafficaction;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.net.TrafficStats;
import android.os.Binder;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

import com.rtwo.trafficmanagement.R;

/**
 * ClassName: TrafficHandle
 * MethodName:
 * Comment:
 * Author:Zhouhao
 * Created on 23:36 2015/8/20
 */
public class TrafficHandle extends Service {

    private int trafficLimitedData = 0;
    public static float trafficData = 0;
    public static float trafficOriginData0;
    public static float trafficOriginData1;
    private int trafficDataOption;
    private TrafficBinder binder = new TrafficBinder();
    private Thread trafficRecord;
    private boolean hasNotice = false;

    public class TrafficBinder extends Binder{
        public float getTrafficData(){
            return trafficData;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v("address","onCreate");
        trafficOriginData1 = (float)(TrafficStats.getTotalRxBytes()/1024.00/1024.00);
        trafficOriginData0 = (float)(TrafficStats.getMobileRxBytes()/1024.00/1024.00);
        trafficRecord = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    switch(trafficDataOption){
                        case 0:
                            trafficData = (float)(TrafficStats.getMobileRxBytes()/1024.00/1024.00)-trafficOriginData0;
                            break;
                        case 1:
                            trafficData = (float)(TrafficStats.getTotalRxBytes()/1024.00/1024.00)-trafficOriginData1;
                    }
                    if(hasNotice == false){
                        if(trafficData > trafficLimitedData){
                            Notification beyondNotice = new Notification(R.drawable.beyondnotice,"您使用的移动数据流量超过了"+trafficLimitedData+"MB",System.currentTimeMillis());
                            beyondNotice.setLatestEventInfo(TrafficHandle.this, "移动数据流量监控", "您使用的移动数据流量超过了" + trafficLimitedData + "MB", null);
                            startForeground(1, beyondNotice);
                            hasNotice = true;
                        }
                    }
                    SystemClock.sleep(100);
                }
            }
        });
        trafficRecord.start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.v("address","onBind");
        trafficLimitedData = Integer.parseInt(intent.getStringExtra("trafficLimitedData"));
        trafficDataOption = intent.getIntExtra("trafficDataOption",1);
        return binder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.v("address","onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.v("address","onDestroy");
        trafficRecord.interrupt();
        super.onDestroy();
    }
}
